/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Alexandre Croiseaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display.gui.form
{
	import flash.display.Shape;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import marcel.display.draw.DrawManager;
	import marcel.display.draw.props.CornerProps;
	import marcel.display.draw.props.FillProps;
	import marcel.display.gui.texts.CoreField;
	import marcel.display.Tween;
	import marcel.display.View;
	
	/**
	 * FormErrorToolTip class used to display error messages associated with an ErrorBadge
	 * @author Alexandre Croiseaux
	 */
	public class FormErrorToolTip extends View
	{
		//--------------------------------------------------------------------------
		//
		//  Private vars
		//
		//--------------------------------------------------------------------------
		private var _txt:CoreField;
		private var _bg:Shape;
		
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Constructs a new FormErrorToolTip instance. This class can be edited directly to change graphics.
		 */
		public function FormErrorToolTip()
		{
			_bg = new Shape();
			_bg.filters = [new DropShadowFilter(4, 45, 0x000000, .5)];
			addChild(_bg);
			
			_txt = new CoreField();
			_txt.setCSSStyle("form_tooltip");
			addChild(_txt);
			
			mouseEnabled = false;
			mouseChildren = false;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Public methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Opens the tooltip
		 * @param	msg	the message to display in the tooltip
		 */
		public function open(msg:String, x:int, y:int):void
		{
			_txt.text = msg;
			
			var p:Point = parent ? parent.localToGlobal(new Point(x, y)) : new Point();
			if (stage && p.x + _txt.width > stage.stageWidth) _txt.x = -_txt.width + 20;
			else _txt.x = 0;
			if (p.y < _txt.height + 25) _txt.y = 25;
			else _txt.y = -_txt.height;
			
			_bg.graphics.clear();
			DrawManager.drawRect(_bg, new Rectangle(_txt.x - 2, _txt.y - 2, _txt.width + 4, _txt.height + 4), new FillProps(0xCB2E2E, .9), null, new CornerProps(5));
			
			this.x = x;
			this.y = y;
			
			alpha = 0;
			Tween.alphaTo(this, 1, 0.2, null, 0, true);
		}
		
		/**
		 * Closes the tooltip
		 */
		public function close():void
		{
			Tween.alphaTo(this, 0, 0.2, null, 0, true);
		}
		
	}

}